<?php
namespace App\Http\Controllers;
use App\Exceptions\ValidateException;
use App\Repository\Contracts\RoleInterface;
use App\Support\Helper\CommonHelper;
use Illuminate\Http\Request;
use Auth;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-01-17 20:11
 *
 * @Description: 角色
 */
class RoleController extends Controller
{
    /**
     * @var Request
     */
    protected $request;

    /**
     * @var RoleInterface
     */
    protected $role;

    protected $uid;

    /**
     * RoleController constructor.
     * @param Request $request
     * @param RoleInterface $role
     */
    public function __construct(Request $request, RoleInterface $role)
    {
        $this->request = $request;

        $this->role = $role;

        $this->uid = CommonHelper::getToken(Auth::user())['uid'];
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $list = $this->role->lists();

        return $this->success($list);
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail($id)
    {
        $detail = $this->role->detail($id);

        return $this->success($detail);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function created()
    {
        try {
            $data = $this->request->only(['name', 'description', 'status']);
            $id = $this->role->created($data);

            return $this->success($id);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }

    /**
     * Update
     *
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function updated($id)
    {
        try {
            $data = $this->request->only(['name', 'description', 'status']);
            $result = $this->role->updated($id, $data);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }
}